<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>信息管理系统</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <link href="/style/basic_layout.css" rel="stylesheet" type="text/css">
    <link href="/style/common_style.css" rel="stylesheet" type="text/css">
    <script type="text/javascript" src="/js/jquery/jquery.js"></script>
    <script type="text/javascript" src="/js/plugins/jquery-form/jquery.form.min.js"></script>
    <script type="text/javascript" src="/js/plugins/My97DatePicker/WdatePicker.js"></script>
    <script type="text/javascript" src="/js/plugins/artDialog/jquery.artDialog.js?skin=blue"></script>
    <script type="text/javascript" src="/js/plugins/artDialog/plugins/iframeTools.js"></script>
</head>
<script type="text/javascript">
    $(function () {
        //给业务时间框绑定事件
        $(":input[name=vdate]").click(function () {
            WdatePicker({readonly: true});
        })
        //改变表单的提交方式
        $("#editForm").ajaxForm(function (data) {
            if (data.success) {
                $.dialog({
                    title: "提示",
                    content: data.msg,
                    ok: function () {
                        location.href = "/stockIncomeBill/list";
                    }
                })
            } else {
                $.dialog({
                    title: "提示",
                    content: data.msg,
                    ok: true
                })
            }
        });
        //添加明细
        $(".appendRow").click(function () {
            //复制第一行
            var clone = $("#edit_table_body tr:first").clone();
            //将内容全部清空
            empty(clone);
            //将这一行加到tbody中
            clone.appendTo($("#edit_table_body"));
        })
        //统一事件绑定
        $("#edit_table_body").on("click", ".searchproduct", function () {
            var tr = $(this).closest("tr");
            $.dialog.open("/product/productListView", {
                title: "选择商品列表",
                width: "90%",
                height: "90%",
                left: "50%",
                top: "50%",
                lock: true,
                resize: false,
                close: function () {
                    var data = $.dialog.data("msg");
                    if (data) {
                        $.dialog.removeData("msg");
                        tr.find("[tag=name]").prop("value", data.name);
                        tr.find("[tag=pid]").prop("value", data.id);
                        tr.find("[tag=brand]").html(data.brandName);
                        var costPrice = tr.find("[tag=costPrice]");
                        costPrice.prop("value", data.costPrice);
                        costPrice.change();
                    }
                }
            }, false);
        }).on("change", "[tag=number],[tag=costPrice]", function () {
            var tr = $(this).closest("tr");
            var number = parseFloat(tr.find("[tag=number]").prop("value") || 0);
            var costPrice = parseFloat(tr.find("[tag=costPrice]").prop("value") || 0);
            var amount = tr.find("[tag=amount]");
            amount.html((number * costPrice).toFixed(2));
        }).on("click", ".removeItem", function () {
            var tr = $(this).closest("tr");
            //拿到tbody中所有的tr
            var trs = $("#edit_table_body tr");
            //如果数量大于1,则删除这一条数据,如果数量为1则把tr内的内容全部清空
            if (trs.size() > 1) {
                tr.remove();
            } else {
                empty(tr);
            }
        });
        //在点击提交按钮实现提交之前将入库单明细的索引未知给加上
        $(".btn_submit").click(function () {
            $("#edit_table_body tr").each(function (index, item) {
                $(item).find("[tag=pid]").prop("name", "items[" + index + "].product.id");
                $(item).find("[tag=costPrice]").prop("name", "items[" + index + "].costPrice");
                $(item).find("[tag=number]").prop("name", "items[" + index + "].number");
                $(item).find("[tag=remark]").prop("name", "items[" + index + "].remark");
            })
            $("#editForm").submit();
        });
    });
    function empty(obj) {
        obj.find("[tag=name]").prop("value", "");
        obj.find("[tag=pid]").prop("value", "");
        obj.find("[tag=brand]").html("");
        obj.find("[tag=costPrice]").prop("value", "");
        obj.find("[tag=number]").prop("value", "");
        obj.find("[tag=amount]").html("");
        obj.find("[tag=remark]").prop("value", "");
    }

</script>
<body>
<form name="editForm" action="/stockIncomeBill/saveOrUpdate" method="post" id="editForm">
    <input type="hidden" name="id" value="${stockIncomeBill.id}">
    <div id="container">
        <div id="nav_links">
            <span style="color: #1A5CC6;">入库单编辑</span>
            <div id="page_close">
                <a>
                    <img src="/images/common/page_close.png" width="20" height="20" style="vertical-align: text-top;"/>
                </a>
            </div>
        </div>
        <div class="ui_content">
            <table cellspacing="0" cellpadding="0" width="100%" align="left" border="0">
                <tr>
                    <td class="ui_text_rt" width="140">入库单编码</td>
                    <td class="ui_text_lt">
                        <input name="sn" value="${stockIncomeBill.sn}" class="ui_input_txt02"/>
                    </td>
                </tr>
                <tr>
                    <td class="ui_text_rt" width="140">仓库</td>
                    <td class="ui_text_lt">
                        <select name="depot.id" class="ui_select01">
                            <c:forEach items="${depots}" var="item">
                                <option value="${item.id}" ${item.id == stockIncomeBill.depot.id ? 'selected' : ''}>${item.name}</option>
                            </c:forEach>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td class="ui_text_rt" width="140">业务时间</td>
                    <td class="ui_text_lt">
                        <fmt:formatDate value="${stockIncomeBill.vdate}" var="vdate"></fmt:formatDate>
                        <input name="vdate" class="ui_input_txt02"
                               value="${vdate}"/>
                    </td>
                </tr>
                <tr>
                    <td class="ui_text_rt" width="140">单据明细</td>
                </tr>
                <tr>
                    <td></td>
                    <td>
                        <input type="button" value="添加明细" class="ui_input_btn01 appendRow"/>
                        <table class="edit_table" cellspacing="0" cellpadding="0" border="0" style="width: auto">
                            <thead>
                            <tr>
                                <th width="10"></th>
                                <th width="200">货品</th>
                                <th width="120">品牌</th>
                                <th width="80">价格</th>
                                <th width="80">数量</th>
                                <th width="80">金额小计</th>
                                <th width="150">备注</th>
                                <th width="60"></th>
                            </tr>
                            </thead>
                            <tbody id="edit_table_body">
                            <c:choose>
                                <c:when test="${empty stockIncomeBill.id}">
                                    <%--显示最开始的一行--%>
                                    <tr>
                                        <td></td>
                                        <td>
                                            <input disabled="true" readonly="true" class="ui_input_txt02" tag="name"/>
                                            <img src="/images/common/search.png" class="searchproduct"/>
                                            <input type="hidden" tag="pid"/>
                                        </td>
                                        <td><span tag="brand"></span></td>
                                        <td><input tag="costPrice"
                                                   class="ui_input_txt02"/></td>
                                        <td><input tag="number"
                                                   class="ui_input_txt02"/></td>
                                        <td><span tag="amount"></span></td>
                                        <td><input tag="remark"
                                                   class="ui_input_txt02"/></td>
                                        <td>
                                            <a href="javascript:;" class="removeItem">删除明细</a>
                                        </td>
                                    </tr>
                                </c:when>
                                <c:otherwise>
                                    <c:forEach items="${stockIncomeBill.items}" var="item">
                                        <tr>
                                            <td></td>
                                            <td>
                                                <input disabled="true" readonly="true" class="ui_input_txt02" tag="name"
                                                       value="${item.product.name}"/>
                                                <img src="/images/common/search.png" class="searchproduct"/>
                                                <input type="hidden" tag="pid" value="${item.product.id}"/>
                                            </td>
                                            <td><span tag="brand">${item.product.brandName}</span></td>
                                            <td><input tag="costPrice"
                                                       class="ui_input_txt02" value="${item.costPrice}"/></td>
                                            <td><input tag="number" type="number" min="1"
                                                       class="ui_input_txt02" value="${item.number}"/></td>
                                            <td><span tag="amount">${item.amount}</span></td>
                                            <td><input tag="remark"
                                                       class="ui_input_txt02" ${item.remark}/></td>
                                            <td>
                                                <a href="javascript:;" class="removeItem">删除明细</a>
                                            </td>
                                        </tr>
                                    </c:forEach>
                                </c:otherwise>
                            </c:choose>

                            </tbody>
                        </table>
                    </td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td class="ui_text_lt">
                        &nbsp;<input type="button" value="确定保存" class="ui_input_btn01 btn_submit"/>
                        &nbsp;<input id="cancelbutton" type="reset" value="重置" class="ui_input_btn01"/>
                    </td>
                </tr>
            </table>
        </div>
    </div>
</form>
</body>
</html>
